package com.pe.cineplanet.proyectocine.repository.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.pe.cineplanet.proyectocine.domain.Cartelera;
import com.pe.cineplanet.proyectocine.domain.Local;
import com.pe.cineplanet.proyectocine.domain.Pelicula;
import com.pe.cineplanet.proyectocine.domain.beans.BusquedasPeliculaLocal;
import com.pe.cineplanet.proyectocine.repository.CarteleraRepository;
 
@Repository
public class CarteleraRepositoryHibernate extends HibernateDaoSupport 
implements CarteleraRepository{

	//constructor
	@Autowired
	public CarteleraRepositoryHibernate(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	public List<Cartelera> obtenerCarteleras() {
		return getHibernateTemplate().find("from Cartelera");   
	}

	public Cartelera obtenerCartelera(int id_Cartelera) {
		return (Cartelera) getSession().createQuery("from Cartelera u where u.id_Cartelera=?").setInteger(0, id_Cartelera).uniqueResult();
	}

	public void grabarCartelera(Cartelera obj) {
		getHibernateTemplate().saveOrUpdate(obj);
		
	}

	public void removerCartelera(int id_Cartelera) {
		Cartelera obj = obtenerCartelera(id_Cartelera);
		getHibernateTemplate().delete(obj);
		
	}

	public List<Cartelera> listarPeliculasxLocal(Local id_Local) {
		//ENVIABA LOCAL
		
		String sql = "from Cartelera c left join fetch c.pelicula p " +
				     "left join fetch c.local l where c.local=?";
		
		//String sql = "from Cartelera c where c.local=?";
		
	/*	String sql = "from Cartelera c left join fetch c.pelicula p " +
				     "left join fetch c.local l where l.id_Local=?";*/
		
	/*	String sql = "p.titulo_Pelicula from cartelera c, pelicula p, local l " +
		              "where c.id_Pelicula = p.id_Pelicula and c.id_Local = l.id_Local " +
		              "and l.id_Local=?";*/
		//List find = getHibernateTemplate().find(sql,desc_Local);
		  return getHibernateTemplate().find(sql,new Object[]{id_Local});
		
	}

	public List<Cartelera> listarPeliculActivasEnCartelera(String activo) {
		/*String sql = "from Cartelera u where u.fl_Activo=?";*/
		
		String sql ="from Cartelera c left join fetch c.pelicula p " +
	     "left join fetch c.local l where c.fl_Activo=?";
		return getHibernateTemplate().find(sql, new Object[]{activo});  
	}

 

}
